package com.sservice.service.service.impl;

import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.commonBusiness.service.CategoryService;
import com.sservice.commonBusiness.vo.CategoryVo;
import com.sservice.model.dao.ICommissionDao;
import com.sservice.model.entity.Commission;
import com.sservice.model.entity.UserWx;
import com.sservice.model.vo.CommissionVo;
import com.sservice.service.service.ICommissionService;
import com.sservice.service.service.IUserWxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CommissionServiceImpl implements ICommissionService {

    @Autowired
    ICommissionDao iCommissionDao;
    @Autowired
    CategoryService categoryService;

    @Override
    public ListResult userCommissionApply(CategoryVo record) {
        List<CategoryVo> list = categoryService.queryWithUserWx(record);
        int size = categoryService.countWithUserWx(record);
        ListResult listResult = new ListResult(true, list);
        listResult.setPage(new Pagination(record.getPage(), size, record.getSize()));
        return listResult;
    }

    /**
     * 佣金历史
     *
     * @param record
     * @return
     */
    public BaseResult userHistoryRecord(CommissionVo record) {
        BaseResult baseResult = new BaseResult(true, list(record));
        baseResult.setPage(new Pagination(record.getPage(), count(record), record.getSize()));
        return baseResult;
    }

    /**
     * @return
     */
    public List<CommissionVo> list(CommissionVo record) {
        CommissionVo commissionVo = new CommissionVo();
        if (record.getSaleId() != null) commissionVo.setSaleId(record.getSaleId());
        commissionVo.setType1(record.getType1() == null ? CommissionVo.BUSINESS_COMMISSION_TYPE1 : record.getType1());   //类型
        if (record.getOrderId() != null) commissionVo.setOrderId(record.getOrderId());                          //关联订单
        commissionVo.setSortField(record.getSortField());
        commissionVo.setSortOrder(record.getSortOrder());
        commissionVo.setSize(record.getSize());
        commissionVo.setPage(record.getPage());
        List<CommissionVo> list = iCommissionDao.queryByPage(commissionVo);
        return list;
    }

    public int count(CommissionVo record) {
        return iCommissionDao.queryCount(record);
    }


    @Autowired
    IUserWxService iUserWxService;

    /**
     * 保存
     *
     * @param commission
     * @return
     */
    @Transactional
    public Commission save(Commission commission) {
        commission.setType1(commission.getType1() == null ? CommissionVo.BUSINESS_COMMISSION_TYPE1 : commission.getType1());   //类型
        giveCommission(Integer.valueOf(commission.getSaleId()),commission.getCommission());
        iCommissionDao.insertSelective(commission);
        return commission;
    }

    /**
     * 红包给到用户
     *
     * @param saleId
     * @param value
     */
    public void giveCommission(Integer saleId, BigDecimal value) {
        UserWx userWx = iUserWxService.selectByPrimaryKey(Integer.valueOf(saleId));
        userWx.setPointsNow(userWx.getPointsNow().add(value));
        userWx.setPoints((userWx.getPoints()).add(value));//
        iUserWxService.updateByPrimaryKeySelective(userWx);
    }

    public Commission existsBy(CommissionVo commission) {
        CommissionVo commission1 = new CommissionVo();
        commission1.setNo(commission.getNo());
        List<CommissionVo> list = iCommissionDao.selectBy(commission1);
        return list.size() == 0 ? null : list.get(0);
    }

    /**
     * 修改内容
     *
     * @param commission
     * @return
     */
    public Commission update(Commission commission) {
        iCommissionDao.updateByPrimaryKeySelective(commission);
        return commission;
    }

}
